package KeShe;

import java.io.*;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TableManager {
    private void Welcome() throws IOException {
        File f=new File(readTableInfo());
        f.createNewFile();
        System.out.println("欢迎使用迷你数据库管理系统! 请使用help命令查询所有操作介绍");
        System.out.print("miDB>");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        br.readLine();
        help();
    }
    //quit指令
    private void quit(){
        System.exit(0);
    }
    //help指令
    private void help(){
        System.out.println("quit ---- 退出迷你数据库管理系统。");
        System.out.println("help ---- 显示所有的指令。");
        System.out.println("show tables -- 显示目前所有数据表。");
        System.out.println("desc XXX -- 显示数据表XXX中的表结构。");
        System.out.println("create table XXX(columnA varchar(10), columnB int, columnC decimal) ---- 创建一个3列的名称为XXX的表格，列名称分别为columnA、columnB、columnC，其类型分别为10个以内的字符、整型数和小数。");
        System.out.println("drop table xxx -- 删除表格XXX。");
        System.out.println("select colX, colY, colX from XXX where colZ > 1.5 order by colZ desc---- 从数据表XXX中选取3列，colX，colY，Z，每一个记录必须满足colZ的值大于1.5 且显示时按照colZ这一列的降序排列。");
        System.out.println("select * from XXX where colA <> '北林信息’ ---- 从数据表XXX中选取所有的列，但记录要满足列colA不是北林信息。");
        System.out.println("insert into XXX values('北林信息', 15, 25.5) ---- 向数据表XXX中追加一条记录，各个列的值分别为北林信息、15、25.5。");
        System.out.println("delete from XXX where colB =10 ---- 把表格XXX中colB列的值是10的记录全部删除。");
        System.out.println("update XXX set colD = '计算机科学与技术' where colA = '北林信息' ---- 在数据表XXX中，把那些colA是北林信息的记录中的colD列全部改写为计算机科学与技术。");
    }
    private static final String readDbHome(){
        Properties properties = new Properties();
        try{
            properties.load(new FileInputStream("src/Keshe/Config.properties"));
            String dbHomePath = properties.getProperty("dbHome");
            return dbHomePath;
        } catch (IOException ioe){
            return null;
        }
    }
    private static final String readTableInfo(){
        Properties properties = new Properties();
        try{
            properties.load(new FileInputStream("src/Keshe/Config.properties"));
            String dbHomePath = properties.getProperty("TI");
            return dbHomePath;
        } catch (IOException ioe){
            return null;
        }
    }
    //show指令
    private void show(){
        File f=new File(readDbHome());
        if (!f.exists()){
            System.out.println("路径不存在！");
            return;
        }
        System.out.println("数据表");
        File fa[]=f.listFiles();
        for (int i=0;i<fa.length;i++){
            File fs=fa[i];
            String str=fs.getName();
            int idx=str.indexOf(".midb");
            System.out.println(str.substring(0,idx));
        }
        System.out.println("共有 "+fa.length+" 个数据表");
    }
    //desc指令
    private void desc(String order,String s) throws IOException {
        String regStr="desc +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            System.out.println("列名称, 列类型, 列宽度");
            int st=m.start(1);
            String classname=s.substring(st);
            FileInputStream in=new FileInputStream(readTableInfo());
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String line=null;
            CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
            while ((line=br.readLine())!=null){
                arr.add(line);
            }
            br.close();
            String[] str;
            for (String ss:arr){
                str=ss.split("\t");
                if (str[0].equals(classname)){
                    System.out.print(str[1]);
                    for (int i=2;i<4;i++){
                        System.out.print(", "+str[i]);
                    }
                    System.out.print("\n");
                }
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //create指令
    private void create(String order,String s) throws IOException {
        String regStr="create +table +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            int st=m.start(1);
            int idx=s.indexOf('(');
            String classname=s.substring(st,idx);
            String[] str=classname.split(" ");
            classname=str[0];
            File f=new File(readDbHome()+"/"+classname+".midb");
            if (!f.createNewFile()){
                System.out.println("数据表已存在");
                return;
            }
            else{
                String tmp=s.substring(idx+1,s.length()-1);
                tmp+=",";
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> col_type=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<Integer> col_wid=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> col_name=new CopyOnWriteArrayList<>();
                String t="";
                for (int i=0;i<tmp.length();i++){
                    if (tmp.charAt(i)==','){
                        arr.add(t);
                        t="";
                    }
                    else{
                        t+=tmp.charAt(i);
                    }
                }
                for (int i=0;i<arr.size();i++){
                    t=arr.get(i);
                    t=" "+t;
                    regStr=" +(.*) +(.*)";
                    p=Pattern.compile(regStr);
                    m=p.matcher(t);
                    if (m.find()){
                        col_name.add(m.group(1));
                        col_type.add(m.group(2));
                        col_wid.add(0);
                    }
                }
                for (int i=0;i<col_type.size();i++){
                    if (col_type.get(i).contains("(")){
                        idx=col_type.get(i).indexOf('(');
                        int idxx=idx;
                        String num="";
                        idx++;
                        while (col_type.get(i).charAt(idx)!=')'){
                            num+=col_type.get(i).charAt(idx);
                            idx++;
                        }
                        col_wid.set(i,Integer.parseInt(num));
                        col_type.set(i,col_type.get(i).substring(0,idxx));
                    }
                    else{
                        col_wid.set(i,1);
                    }
                }
                for (int i=0;i<col_name.size();i++){
                    String tt=col_name.get(i);
                    int j=0;
                    for (;j<tt.length();j++){
                        if (tt.charAt(j)==' '){
                            break;
                        }
                    }
                    tt=tt.substring(0,j);
                    col_name.set(i,tt);
                }
                for (int i=0;i<col_name.size();i++){
                    FileOutputStream out=new FileOutputStream(readTableInfo(),true);
                    byte[] bytes=(classname+"\t"+col_name.get(i)+"\t"+col_type.get(i)+"\t"+col_wid.get(i)+"\n").getBytes();
                    out.write(bytes);
                    out.close();
                }
                System.out.println("创建数据表 "+classname+" 成功！");
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //drop指令
    private void drop(String order,String s) throws IOException {
        String regStr="drop +table +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            int st=m.start(1);
            String classname=s.substring(st);
            File f=new File(readDbHome()+"/"+classname+".midb");
            if (!f.delete()){
                System.out.println("数据表不存在");
                return;
            }
            else{
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                String rewrite="";
                while ((line=br.readLine())!=null){
                    if (line.startsWith(classname)){
                        continue;
                    }
                    rewrite+=line+"\n";
                }
                br.close();
                FileOutputStream out=new FileOutputStream(readTableInfo());
                byte[] bytes=rewrite.getBytes();
                out.write(bytes);
                out.close();
                System.out.println("删除数据表 "+classname+" 成功！");
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //select指令
    private void select(String order,String s) throws IOException, ClassNotFoundException {
        String regStr="select +(.*) +from +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            if (m.group(1).split(" ")[0].equals("*")){
                int idx=m.start(2);
                select_all(m.group(2),s,idx);
            }
            else{
                select_condition(order,s);
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //select 非*指令
    private void select_condition(String order,String s) throws IOException, ClassNotFoundException {
        String regStr="select +(.*) +from +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            String content=m.group(2);
            String content2=s.substring(m.start(2));
            CopyOnWriteArrayList<String> col=new CopyOnWriteArrayList<>();
            CopyOnWriteArrayList<Integer> SIZE=new CopyOnWriteArrayList<>();
            String cols=s.substring(m.start(1),m.end(1));
            cols+=",";
            String t="";
            String tt="";
            for (int i=0;i<cols.length();i++){
                if (cols.charAt(i)!=' '){
                    tt+=cols.charAt(i);
                }
            }
            cols=tt;
            for (int i=0;i<cols.length();i++){
                if (cols.charAt(i)==','){
                    col.add(t);
                    t="";
                }
                else{
                    t+=cols.charAt(i);
                }
            }
            for (int i=0;i<col.size();i++){
                col.set(i,col.get(i).split(" ")[0]);
                SIZE.add(-1);
            }
            String classname=content2.split(" ")[0];
            boolean flag=false;
            File f=new File(readDbHome());
            File fa[]=f.listFiles();
            for (int i=0;i<fa.length;i++){
                File fs=fa[i];
                String fname=fs.getName();
                if (fname.equals(classname+".midb")){
                    flag=true;
                    break;
                }
            }
            if (flag){
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                while ((line=br.readLine())!=null){
                    arr.add(line);
                }
                br.close();
                String[] str;
                CopyOnWriteArrayList<String> title=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> type=new CopyOnWriteArrayList<>();
                for (String ss:arr){
                    str=ss.split("\t");
                    if (str[0].equals(classname)){
                        title.add(str[1]);
                        type.add(str[2]);
                    }
                }
                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> table=new CopyOnWriteArrayList<>();
                FileInputStream fis=new FileInputStream(readDbHome()+"/"+classname+".midb");
                MyObjectInputStream ois=new MyObjectInputStream(fis);
                while (fis.available()>0){
                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                    for (int i=0;i<title.size();i++){
                        tmp.add(ois.readObject());
                    }
                    table.add(tmp);
                }
                ois.close();
                for (int i=0;i<col.size();i++){
                    for (int j=0;j<title.size();j++){
                        if (col.get(i).equals(title.get(j))){
                            SIZE.set(i,j);
                            break;
                        }
                    }
                }
                for (int i=0;i<SIZE.size();i++){
                    if (SIZE.get(i)==-1){
                        System.out.println("列名称不存在！");
                        return;
                    }
                }
                regStr="(.*?) +(.*)";
                p=Pattern.compile(regStr);
                m=p.matcher(content);
                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> sel_table=new CopyOnWriteArrayList<>();
                if (m.find()){
                    content=m.group(2);
                    content2=content2.substring(m.start(2));
                    regStr="where +(.*)";
                    p=Pattern.compile(regStr);
                    m=p.matcher(content);
                    if (m.find()){
                        int st5=m.start(1);
                        content=m.group(1);
                        content2=content2.substring(st5);
                        regStr="(.*) order +by +(.*)";
                        p=Pattern.compile(regStr);
                        m=p.matcher(content);
                        if (m.find()){
                            int ed5=content.indexOf("order");
                            content=content.substring(ed5);
                            String func=content2.substring(ed5);
                            String formula=content2.substring(0,ed5);
                            int i,j;
                            for (i=0;i<formula.length();i++){
                                if (formula.charAt(i)=='<'||formula.charAt(i)=='='||formula.charAt(i)=='>')
                                    break;
                            }
                            String l=formula.substring(0,i);
                            for (j=formula.length()-1;j>=0;j--){
                                if (formula.charAt(j)=='<'||formula.charAt(j)=='='||formula.charAt(j)=='>')
                                    break;
                            }
                            String r=formula.substring(j+1);
                            String condition=formula.substring(i,j+1);
                            formula=l+" "+condition+" "+r;
                            regStr="(.*?) +(.*?) +(.*)";
                            p=Pattern.compile(regStr);
                            m=p.matcher(formula);
                            if (m.find()){
                                String match_title=m.group(1);
                                String value=m.group(3);
                                String op=m.group(2);
                                int idx=0;
                                boolean fl=false;
                                for (int x=0;x<title.size();x++){
                                    if (match_title.equals(title.get(x))){
                                        fl=true;
                                        idx=x;
                                        break;
                                    }
                                }
                                if (fl){
                                    CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                    if (type.get(idx).equals("varchar")){
                                        regStr="'(.*?)'";
                                        p=Pattern.compile(regStr);
                                        m=p.matcher(value);
                                        if (m.find()){
                                            value=m.group(1);
                                            if (op.equals("<")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)<0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)>0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)==0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)>=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)<=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<>")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)!=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else{
                                                System.out.println("无法识别条件运算符！");
                                                return;
                                            }
                                        }
                                        else{
                                            System.out.println("引号格式错误！");
                                            return;
                                        }
                                    }
                                    else{
                                        try{
                                            Double val=Double.parseDouble(value);
                                            if (op.equals("<")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())<val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())>val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())==val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())>=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())<=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<>")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())!=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else{
                                                System.out.println("无法识别条件运算符！");
                                                return;
                                            }
                                        } catch (NumberFormatException e){
                                            System.out.println("格式错误！");
                                            return;
                                        }
                                    }
                                    regStr="order +by +(.*)";
                                    p=Pattern.compile(regStr);
                                    m=p.matcher(content);
                                    if (m.find()){
                                        int st2=m.start(1);
                                        String sort=m.group(1);
                                        String sort2=func.substring(st2);
                                        regStr="(.*) +(.*)";
                                        p=Pattern.compile(regStr);
                                        m=p.matcher(sort);
                                        if (m.find()){
                                            int st3=m.start(1);
                                            int ed3=m.start(2);
                                            String coll=sort2.substring(st3,ed3).split(" ")[0];
                                            if (m.group(2).equals("asc")){
                                                int idxx=-1;
                                                for (int ii=0;ii<title.size();ii++){
                                                    if (title.get(ii).equals(coll)){
                                                        idxx=ii;
                                                        break;
                                                    }
                                                }
                                                if (idxx==-1){
                                                    System.out.println("列名称不存在！");
                                                    return;
                                                }
                                                for (int ii=0;ii<ans.size();ii++){
                                                    for (int jj=0;jj<ans.size();jj++){
                                                        CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                        CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                        if (type.get(idx).equals("varchar")){
                                                            if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())<0){
                                                                ans.set(ii,tmp2);
                                                                ans.set(jj,tmp1);
                                                            }
                                                        }
                                                        else{
                                                            if (Double.parseDouble(tmp1.get(idxx).toString())<Double.parseDouble(tmp2.get(idxx).toString())){
                                                                ans.set(ii,tmp2);
                                                                ans.set(jj,tmp1);
                                                            }
                                                        }
                                                    }
                                                }
                                                for (int ii=0;ii<ans.size();ii++){
                                                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                                    for (int jj=0;jj<col.size();jj++){
                                                        tmp.add(ans.get(ii).get(SIZE.get(jj)));
                                                    }
                                                    sel_table.add(tmp);
                                                }
                                                System.out.print(col.get(0));
                                                for (int ii=1;ii<col.size();ii++){
                                                    System.out.print(", "+col.get(ii));
                                                }
                                                System.out.print("\n");
                                                for (int ii=0;ii<sel_table.size();ii++){
                                                    System.out.print(sel_table.get(ii).get(0));
                                                    for (int jj=1;jj<sel_table.get(ii).size();jj++){
                                                        System.out.print(", "+sel_table.get(ii).get(jj));
                                                    }
                                                    System.out.print("\n");
                                                }
                                                System.out.println("共有 "+sel_table.size()+" 条记录");
                                            }
                                            else if (m.group(2).equals("desc")){
                                                int idxx=-1;
                                                for (int ii=0;ii<title.size();ii++){
                                                    if (title.get(ii).equals(coll)){
                                                        idxx=ii;
                                                        break;
                                                    }
                                                }
                                                if (idxx==-1){
                                                    System.out.println("列名称不存在！");
                                                    return;
                                                }
                                                for (int ii=0;ii<ans.size();ii++){
                                                    for (int jj=0;jj<ans.size();jj++){
                                                        CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                        CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                        if (type.get(idxx).equals("varchar")){
                                                            if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())>0){
                                                                ans.set(ii,tmp2);
                                                                ans.set(jj,tmp1);
                                                            }
                                                        }
                                                        else{
                                                            if (Double.parseDouble(tmp1.get(idxx).toString())>Double.parseDouble(tmp2.get(idxx).toString())){
                                                                ans.set(ii,tmp2);
                                                                ans.set(jj,tmp1);
                                                            }
                                                        }
                                                    }
                                                }
                                                for (int ii=0;ii<ans.size();ii++){
                                                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                                    for (int jj=0;jj<col.size();jj++){
                                                        tmp.add(ans.get(ii).get(SIZE.get(jj)));
                                                    }
                                                    sel_table.add(tmp);
                                                }
                                                System.out.print(col.get(0));
                                                for (int ii=1;ii<col.size();ii++){
                                                    System.out.print(", "+col.get(ii));
                                                }
                                                System.out.print("\n");
                                                for (int ii=0;ii<sel_table.size();ii++){
                                                    System.out.print(sel_table.get(ii).get(0));
                                                    for (int jj=1;jj<sel_table.get(ii).size();jj++){
                                                        System.out.print(", "+sel_table.get(ii).get(jj));
                                                    }
                                                    System.out.print("\n");
                                                }
                                                System.out.println("共有 "+sel_table.size()+" 条记录");
                                            }
                                            else{
                                                System.out.println("指令错误，无法识别！");
                                                return;
                                            }
                                        }
                                        else{
                                            int idxx=-1;
                                            for (int ii=0;ii<title.size();ii++){
                                                if (title.get(ii).equals(sort2)){
                                                    idxx=ii;
                                                    break;
                                                }
                                            }
                                            if (idxx==-1){
                                                System.out.println("列名称不存在！");
                                                return;
                                            }
                                            for (int ii=0;ii<ans.size();ii++){
                                                for (int jj=0;jj<ans.size();jj++){
                                                    CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                    CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                    if (type.get(idxx).equals("varchar")){
                                                        if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())<0){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                    else{
                                                        if (Double.parseDouble(tmp1.get(idxx).toString())<Double.parseDouble(tmp2.get(idxx).toString())){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                }
                                            }
                                            for (int ii=0;ii<ans.size();ii++){
                                                CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                                for (int jj=0;jj<col.size();jj++){
                                                    tmp.add(ans.get(ii).get(SIZE.get(jj)));
                                                }
                                                sel_table.add(tmp);
                                            }
                                            System.out.print(col.get(0));
                                            for (int ii=1;ii<col.size();ii++){
                                                System.out.print(", "+col.get(ii));
                                            }
                                            System.out.print("\n");
                                            for (int ii=0;ii<sel_table.size();ii++){
                                                System.out.print(sel_table.get(ii).get(0));
                                                for (int jj=1;jj<sel_table.get(ii).size();jj++){
                                                    System.out.print(", "+sel_table.get(ii).get(jj));
                                                }
                                                System.out.print("\n");
                                            }
                                            System.out.println("共有 "+sel_table.size()+" 条记录");
                                        }
                                    }
                                    else{
                                        System.out.println("格式错误！");
                                        return;
                                    }
                                }
                                else{
                                    System.out.println("列名称不存在！");
                                    return;
                                }
                            }
                            else{
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        else{
                            String formula=content2;
                            int i,j;
                            for (i=0;i<formula.length();i++){
                                if (formula.charAt(i)=='<'||formula.charAt(i)=='='||formula.charAt(i)=='>')
                                    break;
                            }
                            String l=formula.substring(0,i);
                            for (j=formula.length()-1;j>=0;j--){
                                if (formula.charAt(j)=='<'||formula.charAt(j)=='='||formula.charAt(j)=='>')
                                    break;
                            }
                            String r=formula.substring(j+1);
                            String condition=formula.substring(i,j+1);
                            formula=l+" "+condition+" "+r;
                            regStr="(.*?) +(.*?) +(.*)";
                            p=Pattern.compile(regStr);
                            m=p.matcher(formula);
                            if (m.find()){
                                String match_title=m.group(1);
                                String value=m.group(3);
                                String op=m.group(2);
                                int idx=0;
                                boolean fl=false;
                                for (int x=0;x<title.size();x++){
                                    if (match_title.equals(title.get(x))){
                                        fl=true;
                                        idx=x;
                                        break;
                                    }
                                }
                                if (fl){
                                    CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                    if (type.get(idx).equals("varchar")){
                                        regStr="'(.*?)'";
                                        p=Pattern.compile(regStr);
                                        m=p.matcher(value);
                                        if (m.find()){
                                            value=m.group(1);
                                            if (op.equals("<")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)<0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)>0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)==0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)>=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)<=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<>")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (tmp.get(idx).toString().compareTo(value)!=0){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else{
                                                System.out.println("无法识别条件运算符！");
                                                return;
                                            }
                                        }
                                        else{
                                            System.out.println("引号格式错误！");
                                            return;
                                        }
                                    }
                                    else{
                                        try{
                                            Double val=Double.parseDouble(value);
                                            if (op.equals("<")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())<val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())>val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())==val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals(">=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())>=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<=")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())<=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else if (op.equals("<>")){
                                                for (int x=0;x<table.size();x++){
                                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                    if (Double.parseDouble(tmp.get(idx).toString())!=val){
                                                        ans.add(tmp);
                                                    }
                                                }
                                            }
                                            else{
                                                System.out.println("无法识别条件运算符！");
                                                return;
                                            }
                                        } catch (NumberFormatException e){
                                            System.out.println("格式错误！");
                                            return;
                                        }
                                    }
                                    for (int ii=0;ii<ans.size();ii++){
                                        CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                        for (int jj=0;jj<col.size();jj++){
                                            tmp.add(ans.get(ii).get(SIZE.get(jj)));
                                        }
                                        sel_table.add(tmp);
                                    }
                                    System.out.print(col.get(0));
                                    for (int ii=1;ii<col.size();ii++){
                                        System.out.print(", "+col.get(ii));
                                    }
                                    System.out.print("\n");
                                    for (int ii=0;ii<sel_table.size();ii++){
                                        System.out.print(sel_table.get(ii).get(0));
                                        for (int jj=1;jj<sel_table.get(ii).size();jj++){
                                            System.out.print(", "+sel_table.get(ii).get(jj));
                                        }
                                        System.out.print("\n");
                                    }
                                    System.out.println("共有 "+sel_table.size()+" 条记录");
                                }
                                else{
                                    System.out.println("列名称不存在！");
                                    return;
                                }
                            }
                            else{
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                    }
                    else{
                        regStr="order +by +(.*)";
                        p=Pattern.compile(regStr);
                        m=p.matcher(content);
                        if (m.find()){
                            int st2=m.start(1);
                            String sort=m.group(1);
                            String sort2=content2.substring(st2);
                            regStr="(.*) +(.*)";
                            p=Pattern.compile(regStr);
                            m=p.matcher(sort);
                            if (m.find()){
                                int st3=m.start(1);
                                int ed3=m.start(2);
                                String coll=sort2.substring(st3,ed3).split(" ")[0];
                                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                ans=table;
                                if (m.group(2).equals("asc")){
                                    int idx=-1;
                                    for (int i=0;i<title.size();i++){
                                        if (title.get(i).equals(coll)){
                                            idx=i;
                                            break;
                                        }
                                    }
                                    if (idx==-1){
                                        System.out.println("列名称不存在！");
                                        return;
                                    }
                                    for (int i=0;i<ans.size();i++){
                                        for (int j=0;j<ans.size();j++){
                                            CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                            CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                            if (type.get(idx).equals("varchar")){
                                                if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())<0){
                                                    ans.set(i,tmp2);
                                                    ans.set(j,tmp1);
                                                }
                                            }
                                            else{
                                                if (Double.parseDouble(tmp1.get(idx).toString())<Double.parseDouble(tmp2.get(idx).toString())){
                                                    ans.set(i,tmp2);
                                                    ans.set(j,tmp1);
                                                }
                                            }
                                        }
                                    }
                                    for (int i=0;i<ans.size();i++){
                                        CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                        for (int j=0;j<col.size();j++){
                                            tmp.add(ans.get(i).get(SIZE.get(j)));
                                        }
                                        sel_table.add(tmp);
                                    }
                                    System.out.print(col.get(0));
                                    for (int i=1;i<col.size();i++){
                                        System.out.print(", "+col.get(i));
                                    }
                                    System.out.print("\n");
                                    for (int i=0;i<sel_table.size();i++){
                                        System.out.print(sel_table.get(i).get(0));
                                        for (int j=1;j<sel_table.get(i).size();j++){
                                            System.out.print(", "+sel_table.get(i).get(j));
                                        }
                                        System.out.print("\n");
                                    }
                                    System.out.println("共有 "+sel_table.size()+" 条记录");
                                }
                                else if (m.group(2).equals("desc")){
                                    int idx=-1;
                                    for (int i=0;i<title.size();i++){
                                        if (title.get(i).equals(coll)){
                                            idx=i;
                                            break;
                                        }
                                    }
                                    if (idx==-1){
                                        System.out.println("列名称不存在！");
                                        return;
                                    }
                                    for (int i=0;i<ans.size();i++){
                                        for (int j=0;j<ans.size();j++){
                                            CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                            CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                            if (type.get(idx).equals("varchar")){
                                                if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())>0){
                                                    ans.set(i,tmp2);
                                                    ans.set(j,tmp1);
                                                }
                                            }
                                            else{
                                                if (Double.parseDouble(tmp1.get(idx).toString())>Double.parseDouble(tmp2.get(idx).toString())){
                                                    ans.set(i,tmp2);
                                                    ans.set(j,tmp1);
                                                }
                                            }
                                        }
                                    }
                                    for (int i=0;i<ans.size();i++){
                                        CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                        for (int j=0;j<col.size();j++){
                                            tmp.add(ans.get(i).get(SIZE.get(j)));
                                        }
                                        sel_table.add(tmp);
                                    }
                                    System.out.print(col.get(0));
                                    for (int i=1;i<col.size();i++){
                                        System.out.print(", "+col.get(i));
                                    }
                                    System.out.print("\n");
                                    for (int i=0;i<sel_table.size();i++){
                                        System.out.print(sel_table.get(i).get(0));
                                        for (int j=1;j<sel_table.get(i).size();j++){
                                            System.out.print(", "+sel_table.get(i).get(j));
                                        }
                                        System.out.print("\n");
                                    }
                                    System.out.println("共有 "+sel_table.size()+" 条记录");
                                }
                                else{
                                    System.out.println("指令错误，无法识别！");
                                    return;
                                }
                            }
                            else{
                                int idx=-1;
                                for (int i=0;i<title.size();i++){
                                    if (title.get(i).equals(sort2.split(" ")[0])){
                                        idx=i;
                                        break;
                                    }
                                }
                                if (idx==-1){
                                    System.out.println("列名称不存在！");
                                    return;
                                }
                                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                ans=table;
                                for (int i=0;i<ans.size();i++){
                                    for (int j=0;j<ans.size();j++){
                                        CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                        CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                        if (type.get(idx).equals("varchar")){
                                            if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())<0){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                        else{
                                            if (Double.parseDouble(tmp1.get(idx).toString())<Double.parseDouble(tmp2.get(idx).toString())){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                    }
                                }
                                for (int i=0;i<ans.size();i++){
                                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                                    for (int j=0;j<col.size();j++){
                                        tmp.add(ans.get(i).get(SIZE.get(j)));
                                    }
                                    sel_table.add(tmp);
                                }
                                System.out.print(col.get(0));
                                for (int i=1;i<col.size();i++){
                                    System.out.print(", "+col.get(i));
                                }
                                System.out.print("\n");
                                for (int i=0;i<sel_table.size();i++){
                                    System.out.print(sel_table.get(i).get(0));
                                    for (int j=1;j<sel_table.get(i).size();j++){
                                        System.out.print(", "+sel_table.get(i).get(j));
                                    }
                                    System.out.print("\n");
                                }
                                System.out.println("共有 "+sel_table.size()+" 条记录");
                            }
                        }
                        else{
                            System.out.println("格式错误！");
                            return;
                        }
                    }
                }
                else{
                    if (!content.contains(" ")){
                        System.out.print(col.get(0));
                        for (int i=1;i<col.size();i++){
                            System.out.print(", "+col.get(i));
                        }

                        System.out.print("\n");
                        for (int i=0;i<sel_table.size();i++){
                            System.out.print(sel_table.get(i).get(0));
                            for (int j=1;j<sel_table.get(i).size();j++){
                                System.out.print(", "+sel_table.get(i).get(j));
                            }
                            System.out.print("\n");
                        }
                        System.out.println("共有 "+sel_table.size()+" 条记录");
                    }
                    else{
                        System.out.println("格式错误！");
                        return;
                    }
                }
            }
            else{
                System.out.println("数据表不存在！");
                return;
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //select *指令
    private void select_all(String order,String s,int start) throws IOException, ClassNotFoundException {
        String regStr="(.*?) +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        // from xxx xxxxxxxxxxx
        if (m.find()){
            int st1=m.start(1)+start;
            int ed1=m.start(2)+start;
            String classname=s.substring(st1,ed1).split(" ")[0];
            boolean flag=false;
            File f=new File(readDbHome());
            File fa[]=f.listFiles();
            for (int i=0;i<fa.length;i++){
                File fs=fa[i];
                String fname=fs.getName();
                if (fname.equals(classname+".midb")){
                    flag=true;
                    break;
                }
            }
            //xxx 存在
            if (flag){
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                while ((line=br.readLine())!=null){
                    arr.add(line);
                }
                br.close();
                String[] str;
                CopyOnWriteArrayList<String> title=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> type=new CopyOnWriteArrayList<>();
                for (String ss:arr){
                    str=ss.split("\t");
                    if (str[0].equals(classname)){
                        title.add(str[1]);
                        type.add(str[2]);
                    }
                }
                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> table=new CopyOnWriteArrayList<>();
                FileInputStream fis=new FileInputStream(readDbHome()+"/"+classname+".midb");
                MyObjectInputStream ois=new MyObjectInputStream(fis);
                while (fis.available()>0){
                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                    for (int i=0;i<title.size();i++){
                        tmp.add(ois.readObject());
                    }
                    table.add(tmp);
                }
                ois.close();
                //将xxx数据表的内容存入table数组
                String content=m.group(2);
                String content2=s.substring(ed1);
                regStr="where +(.*)";
                p=Pattern.compile(regStr);
                m=p.matcher(content);
                //where xxxxxxxx
                if (m.find()){
                    int st5=m.start(1);
                    content=m.group(1);
                    content2=content2.substring(st5);
                    regStr="(.*) order +by +(.*)";
                    p=Pattern.compile(regStr);
                    m=p.matcher(content);
                    if (m.find()){
                        int ed5=content.indexOf("order");
                        content=content.substring(ed5);
                        String func=content2.substring(ed5);
                        String formula=content2.substring(0,ed5);
                        int i,j;
                        for (i=0;i<formula.length();i++){
                            if (formula.charAt(i)=='<'||formula.charAt(i)=='='||formula.charAt(i)=='>')
                                break;
                        }
                        String l=formula.substring(0,i);
                        for (j=formula.length()-1;j>=0;j--){
                            if (formula.charAt(j)=='<'||formula.charAt(j)=='='||formula.charAt(j)=='>')
                                break;
                        }
                        String r=formula.substring(j+1);
                        String condition=formula.substring(i,j+1);
                        formula=l+" "+condition+" "+r;
                        regStr="(.*?) +(.*?) +(.*)";
                        p=Pattern.compile(regStr);
                        m=p.matcher(formula);
                        if (m.find()){
                            String match_title=m.group(1);
                            String value=m.group(3);
                            String op=m.group(2);
                            int idx=0;
                            boolean fl=false;
                            for (int x=0;x<title.size();x++){
                                if (match_title.equals(title.get(x))){
                                    fl=true;
                                    idx=x;
                                    break;
                                }
                            }
                            if (fl){
                                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                if (type.get(idx).equals("varchar")){
                                    regStr="'(.*?)'";
                                    p=Pattern.compile(regStr);
                                    m=p.matcher(value);
                                    if (m.find()){
                                        value=m.group(1);
                                        if (op.equals("<")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)<0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)>0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)==0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)>=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)<=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<>")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)!=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else{
                                            System.out.println("无法识别条件运算符！");
                                            return;
                                        }
                                    }
                                    else{
                                        System.out.println("引号格式错误！");
                                        return;
                                    }
                                }
                                else{
                                    try{
                                        Double val=Double.parseDouble(value);
                                        if (op.equals("<")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())<val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())>val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())==val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())>=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())<=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<>")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())!=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else{
                                            System.out.println("无法识别条件运算符！");
                                            return;
                                        }
                                    } catch (NumberFormatException e){
                                        System.out.println("格式错误！");
                                        return;
                                    }
                                }
                                regStr="order +by +(.*)";
                                p=Pattern.compile(regStr);
                                m=p.matcher(content);
                                if (m.find()){
                                    int st2=m.start(1);
                                    String sort=m.group(1);
                                    String sort2=func.substring(st2);
                                    regStr="(.*) +(.*)";
                                    p=Pattern.compile(regStr);
                                    m=p.matcher(sort);
                                    if (m.find()){
                                        int st3=m.start(1);
                                        int ed3=m.start(2);
                                        String col=sort2.substring(st3,ed3).split(" ")[0];
                                        if (m.group(2).equals("asc")){
                                            int idxx=-1;
                                            for (int ii=0;ii<title.size();ii++){
                                                if (title.get(ii).equals(col)){
                                                    idxx=ii;
                                                    break;
                                                }
                                            }
                                            if (idxx==-1){
                                                System.out.println("列名称不存在！");
                                                return;
                                            }
                                            for (int ii=0;ii<ans.size();ii++){
                                                for (int jj=0;jj<ans.size();jj++){
                                                    CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                    CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                    if (type.get(idx).equals("varchar")){
                                                        if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())<0){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                    else{
                                                        if (Double.parseDouble(tmp1.get(idxx).toString())<Double.parseDouble(tmp2.get(idxx).toString())){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                }
                                            }
                                            System.out.print(title.get(0));
                                            for (int ii=1;ii<title.size();ii++){
                                                System.out.print(", "+title.get(ii));
                                            }
                                            System.out.print("\n");
                                            for (int ii=0;ii<ans.size();ii++){
                                                System.out.print(ans.get(ii).get(0));
                                                for (int jj=1;jj<ans.get(ii).size();jj++){
                                                    System.out.print(", "+ans.get(ii).get(jj));
                                                }
                                                System.out.print("\n");
                                            }
                                            System.out.println("共有 "+ans.size()+" 条记录");
                                        }
                                        else if (m.group(2).equals("desc")){
                                            int idxx=-1;
                                            for (int ii=0;ii<title.size();ii++){
                                                if (title.get(ii).equals(col)){
                                                    idxx=ii;
                                                    break;
                                                }
                                            }
                                            if (idxx==-1){
                                                System.out.println("列名称不存在！");
                                                return;
                                            }
                                            for (int ii=0;ii<ans.size();ii++){
                                                for (int jj=0;jj<ans.size();jj++){
                                                    CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                    CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                    if (type.get(idxx).equals("varchar")){
                                                        if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())>0){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                    else{
                                                        if (Double.parseDouble(tmp1.get(idxx).toString())>Double.parseDouble(tmp2.get(idxx).toString())){
                                                            ans.set(ii,tmp2);
                                                            ans.set(jj,tmp1);
                                                        }
                                                    }
                                                }
                                            }
                                            System.out.print(title.get(0));
                                            for (int ii=1;ii<title.size();ii++){
                                                System.out.print(", "+title.get(ii));
                                            }
                                            System.out.print("\n");
                                            for (int ii=0;ii<ans.size();ii++){
                                                System.out.print(ans.get(ii).get(0));
                                                for (int jj=1;jj<ans.get(ii).size();jj++){
                                                    System.out.print(", "+ans.get(ii).get(jj));
                                                }
                                                System.out.print("\n");
                                            }
                                            System.out.println("共有 "+ans.size()+" 条记录");
                                        }
                                        else{
                                            System.out.println("指令错误，无法识别！");
                                            return;
                                        }
                                    }
                                    else{
                                        int idxx=-1;
                                        for (int ii=0;ii<title.size();ii++){
                                            if (title.get(ii).equals(sort2)){
                                                idxx=ii;
                                                break;
                                            }
                                        }
                                        if (idxx==-1){
                                            System.out.println("列名称不存在！");
                                            return;
                                        }
                                        for (int ii=0;ii<ans.size();ii++){
                                            for (int jj=0;jj<ans.size();jj++){
                                                CopyOnWriteArrayList<Object> tmp1=ans.get(ii);
                                                CopyOnWriteArrayList<Object> tmp2=ans.get(jj);
                                                if (type.get(idxx).equals("varchar")){
                                                    if (tmp1.get(idxx).toString().compareTo(tmp2.get(idxx).toString())<0){
                                                        ans.set(ii,tmp2);
                                                        ans.set(jj,tmp1);
                                                    }
                                                }
                                                else{
                                                    if (Double.parseDouble(tmp1.get(idxx).toString())<Double.parseDouble(tmp2.get(idxx).toString())){
                                                        ans.set(ii,tmp2);
                                                        ans.set(jj,tmp1);
                                                    }
                                                }
                                            }
                                        }
                                        System.out.print(title.get(0));
                                        for (int ii=1;ii<title.size();ii++){
                                            System.out.print(", "+title.get(ii));
                                        }
                                        System.out.print("\n");
                                        for (int ii=0;ii<ans.size();ii++){
                                            System.out.print(ans.get(ii).get(0));
                                            for (int jj=1;jj<ans.get(ii).size();jj++){
                                                System.out.print(", "+ans.get(ii).get(jj));
                                            }
                                            System.out.print("\n");
                                        }
                                        System.out.println("共有 "+ans.size()+" 条记录");
                                    }
                                }
                                else{
                                    System.out.println("格式错误！");
                                    return;
                                }
                            }
                            else{
                                System.out.println("列名称不存在！");
                                return;
                            }
                        }
                        else{
                            System.out.println("格式错误！");
                            return;
                        }
                    }
                    //只有 where 语句
                    else{
                        String formula=content2;
                        int i,j;
                        for (i=0;i<formula.length();i++){
                            if (formula.charAt(i)=='<'||formula.charAt(i)=='='||formula.charAt(i)=='>')
                                break;
                        }
                        String l=formula.substring(0,i);
                        for (j=formula.length()-1;j>=0;j--){
                            if (formula.charAt(j)=='<'||formula.charAt(j)=='='||formula.charAt(j)=='>')
                                break;
                        }
                        String r=formula.substring(j+1);
                        String condition=formula.substring(i,j+1);
                        formula=l+" "+condition+" "+r;
                        regStr="(.*?) +(.*?) +(.*)";
                        p=Pattern.compile(regStr);
                        m=p.matcher(formula);
                        if (m.find()){
                            String match_title=m.group(1);
                            String value=m.group(3);
                            String op=m.group(2);
                            int idx=0;
                            boolean fl=false;
                            for (int x=0;x<title.size();x++){
                                if (match_title.equals(title.get(x))){
                                    fl=true;
                                    idx=x;
                                    break;
                                }
                            }
                            if (fl){
                                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                                if (type.get(idx).equals("varchar")){
                                    regStr="'(.*?)'";
                                    p=Pattern.compile(regStr);
                                    m=p.matcher(value);
                                    if (m.find()){
                                        value=m.group(1);
                                        if (op.equals("<")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)<0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)>0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)==0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)>=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)<=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<>")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (tmp.get(idx).toString().compareTo(value)!=0){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else{
                                            System.out.println("无法识别条件运算符！");
                                            return;
                                        }
                                    }
                                    else{
                                        System.out.println("引号格式错误！");
                                        return;
                                    }
                                }
                                else{
                                    try{
                                        Double val=Double.parseDouble(value);
                                        if (op.equals("<")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())<val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())>val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())==val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals(">=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())>=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<=")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())<=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else if (op.equals("<>")){
                                            for (int x=0;x<table.size();x++){
                                                CopyOnWriteArrayList<Object> tmp=table.get(x);
                                                if (Double.parseDouble(tmp.get(idx).toString())!=val){
                                                    ans.add(tmp);
                                                }
                                            }
                                        }
                                        else{
                                            System.out.println("无法识别条件运算符！");
                                            return;
                                        }
                                    } catch (NumberFormatException e){
                                        System.out.println("格式错误！");
                                        return;
                                    }
                                }
                                System.out.print(title.get(0));
                                for (int x=1;x<title.size();x++){
                                    System.out.print(", "+title.get(x));
                                }
                                System.out.print("\n");
                                for (int x=0;x<ans.size();x++){
                                    System.out.print(ans.get(x).get(0));
                                    for (int y=1;y<ans.get(x).size();y++){
                                        System.out.print(", "+ans.get(x).get(y));
                                    }
                                    System.out.print("\n");
                                }
                                System.out.println("共有 "+ans.size()+" 条记录");
                            }
                            else{
                                System.out.println("列名称不存在！");
                                return;
                            }
                        }
                        else{
                            System.out.println("格式错误！");
                            return;
                        }
                    }
                }
                //order by xxxxxx
                else{
                    regStr="order +by +(.*)";
                    p=Pattern.compile(regStr);
                    m=p.matcher(content);
                    //可以匹配 order by
                    if (m.find()){
                        int st2=m.start(1);
                        String sort=m.group(1);
                        String sort2=content2.substring(st2);
                        regStr="(.*) +(.*)";
                        p=Pattern.compile(regStr);
                        m=p.matcher(sort);
                        if (m.find()){
                            int st3=m.start(1);
                            int ed3=m.start(2);
                            String col=sort2.substring(st3,ed3).split(" ")[0];
                            CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                            ans=table;
                            if (m.group(2).equals("asc")){
                                int idx=-1;
                                for (int i=0;i<title.size();i++){
                                    if (title.get(i).equals(col)){
                                        idx=i;
                                        break;
                                    }
                                }
                                if (idx==-1){
                                    System.out.println("列名称不存在！");
                                    return;
                                }
                                for (int i=0;i<ans.size();i++){
                                    for (int j=0;j<ans.size();j++){
                                        CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                        CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                        if (type.get(idx).equals("varchar")){
                                            if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())<0){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                        else{
                                            if (Double.parseDouble(tmp1.get(idx).toString())<Double.parseDouble(tmp2.get(idx).toString())){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                    }
                                }
                                System.out.print(title.get(0));
                                for (int i=1;i<title.size();i++){
                                    System.out.print(", "+title.get(i));
                                }
                                System.out.print("\n");
                                for (int i=0;i<ans.size();i++){
                                    System.out.print(ans.get(i).get(0));
                                    for (int j=1;j<ans.get(i).size();j++){
                                        System.out.print(", "+ans.get(i).get(j));
                                    }
                                    System.out.print("\n");
                                }
                                System.out.println("共有 "+ans.size()+" 条记录");
                            }
                            else if (m.group(2).equals("desc")){
                                int idx=-1;
                                for (int i=0;i<title.size();i++){
                                    if (title.get(i).equals(col)){
                                        idx=i;
                                        break;
                                    }
                                }
                                if (idx==-1){
                                    System.out.println("列名称不存在！");
                                    return;
                                }
                                for (int i=0;i<ans.size();i++){
                                    for (int j=0;j<ans.size();j++){
                                        CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                        CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                        if (type.get(idx).equals("varchar")){
                                            if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())>0){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                        else{
                                            if (Double.parseDouble(tmp1.get(idx).toString())>Double.parseDouble(tmp2.get(idx).toString())){
                                                ans.set(i,tmp2);
                                                ans.set(j,tmp1);
                                            }
                                        }
                                    }
                                }
                                System.out.print(title.get(0));
                                for (int i=1;i<title.size();i++){
                                    System.out.print(", "+title.get(i));
                                }
                                System.out.print("\n");
                                for (int i=0;i<ans.size();i++){
                                    System.out.print(ans.get(i).get(0));
                                    for (int j=1;j<ans.get(i).size();j++){
                                        System.out.print(", "+ans.get(i).get(j));
                                    }
                                    System.out.print("\n");
                                }
                                System.out.println("共有 "+ans.size()+" 条记录");
                            }
                            else{
                                System.out.println("指令错误，无法识别！");
                                return;
                            }
                        }
                        else{
                            int idx=-1;
                            for (int i=0;i<title.size();i++){
                                if (title.get(i).equals(sort2)){
                                    idx=i;
                                    break;
                                }
                            }
                            if (idx==-1){
                                System.out.println("列名称不存在！");
                                return;
                            }
                            CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                            ans=table;
                            for (int i=0;i<ans.size();i++){
                                for (int j=0;j<ans.size();j++){
                                    CopyOnWriteArrayList<Object> tmp1=ans.get(i);
                                    CopyOnWriteArrayList<Object> tmp2=ans.get(j);
                                    if (type.get(idx).equals("varchar")){
                                        if (tmp1.get(idx).toString().compareTo(tmp2.get(idx).toString())<0){
                                            ans.set(i,tmp2);
                                            ans.set(j,tmp1);
                                        }
                                    }
                                    else{
                                        if (Double.parseDouble(tmp1.get(idx).toString())<Double.parseDouble(tmp2.get(idx).toString())){
                                            ans.set(i,tmp2);
                                            ans.set(j,tmp1);
                                        }
                                    }
                                }
                            }
                            System.out.print(title.get(0));
                            for (int i=1;i<title.size();i++){
                                System.out.print(", "+title.get(i));
                            }
                            System.out.print("\n");
                            for (int i=0;i<ans.size();i++){
                                System.out.print(ans.get(i).get(0));
                                for (int j=1;j<ans.get(i).size();j++){
                                    System.out.print(", "+ans.get(i).get(j));
                                }
                                System.out.print("\n");
                            }
                            System.out.println("共有 "+ans.size()+" 条记录");
                        }
                    }
                    else{
                        System.out.println("格式错误！");
                        return;
                    }
                }
            }
            //xxx 不存在
            else{
                System.out.println("数据表不存在！");
                return;
            }
        }
        //from xxx
        else{
            if (!order.contains(" ")){
                String classname=s.substring(start).split("  ")[0];
                boolean flag=false;
                File f=new File(readDbHome());
                File fa[]=f.listFiles();
                for (int i=0;i<fa.length;i++){
                    File fs=fa[i];
                    String fname=fs.getName();
                    if (fname.equals(classname+".midb")){
                        flag=true;
                        break;
                    }
                }
                if (flag){
                    FileInputStream in=new FileInputStream(readTableInfo());
                    BufferedReader br=new BufferedReader(new InputStreamReader(in));
                    String line=null;
                    CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                    while ((line=br.readLine())!=null){
                        arr.add(line);
                    }
                    br.close();
                    String[] str;
                    CopyOnWriteArrayList<String> title=new CopyOnWriteArrayList<>();
                    for (String ss:arr){
                        str=ss.split("\t");
                        if (str[0].equals(classname)){
                            title.add(str[1]);
                        }
                    }
                    CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> table=new CopyOnWriteArrayList<>();
                    FileInputStream fis=new FileInputStream(readDbHome()+"/"+classname+".midb");
                    MyObjectInputStream ois=new MyObjectInputStream(fis);
                    while (fis.available()>0){
                        CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                        for (int i=0;i<title.size();i++){
                            tmp.add(ois.readObject());
                        }
                        table.add(tmp);
                    }
                    ois.close();
                    CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=table;
                    System.out.print(title.get(0));
                    for (int i=1;i<title.size();i++){
                        System.out.print(", "+title.get(i));
                    }
                    System.out.print("\n");
                    for (int i=0;i<ans.size();i++){
                        System.out.print(ans.get(i).get(0));
                        for (int j=1;j<ans.get(i).size();j++){
                            System.out.print(", "+ans.get(i).get(j));
                        }
                        System.out.print("\n");
                    }
                    System.out.println("共有 "+ans.size()+" 条记录");
                }
                else{
                    System.out.println("数据表不存在！");
                    return;
                }
            }
            else{
                System.out.println("格式错误！");
            }
        }
    }
    //insert指令
    private void insert(String order,String s) throws IOException {
        int idx=order.indexOf("values");
        if (idx==-1){
            System.out.println("格式错误！");
            return;
        }
        String tmp1=order.substring(0,idx+6);
        String tmp2=order.substring(idx+6);
        order=tmp1+" "+tmp2;
        String regStr="insert +into +(.*) +values +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()&&s.charAt(s.length()-1)==')'){
            int st1=m.start(1);
            int ed1=order.indexOf("values");
            String classname=s.substring(st1,ed1);
            String[] str=classname.split(" ");
            classname=str[0];
            boolean flag=false;
            File f=new File(readDbHome());
            File fa[]=f.listFiles();
            for (int i=0;i<fa.length;i++){
                File fs=fa[i];
                String fname=fs.getName();
                if (fname.equals(classname+".midb")){
                    flag=true;
                    break;
                }
            }
            if (flag){
                int st=m.start(2);
                String content=s.substring(st,s.indexOf(')'));
                content+=",";
                CopyOnWriteArrayList<String> text=new CopyOnWriteArrayList<>();
                String t="";
                for (int i=0;i<content.length();i++){
                    if (content.charAt(i)==','){
                        text.add(t);
                        t="";
                    }
                    else{
                        t+=content.charAt(i);
                    }
                }
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                while ((line=br.readLine())!=null){
                    arr.add(line);
                }
                br.close();
                String[] str2;
                CopyOnWriteArrayList<String> type=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<Integer> wid=new CopyOnWriteArrayList<>();
                for (String ss:arr){
                    str2=ss.split("\t");
                    if (str2[0].equals(classname)){
                        if (str2[2].equals("int")){
                            type.add("Integer");
                            wid.add(1);
                        }
                        else if (str2[2].equals("decimal")){
                            type.add("Double");
                            wid.add(1);
                        }
                        else if (str2[2].equals("varchar")){
                            type.add("String");
                            wid.add(Integer.parseInt(str2[3]));
                        }
                    }
                }
                Object[] obj=new Object[text.size()+1];
                for (int i=0;i<text.size();i++){
                    if (type.get(i).equals("Integer")){
                        String tt="";
                        for (int ii=0;ii<text.get(i).length();ii++){
                            if (text.get(i).charAt(ii)!=' '){
                                tt+=text.get(i).charAt(ii);
                            }
                        }
                        text.set(i,tt);
                        try{
                            obj[i]=Integer.parseInt(text.get(i));
                        } catch (NumberFormatException e){
                            int pos=i+1;
                            System.out.println("你的第 "+pos+" 个值不能转换成整数！");
                            return;
                        }
                    }
                    else if (type.get(i).equals("Double")){
                        String tt="";
                        for (int ii=0;ii<text.get(i).length();ii++){
                            if (text.get(i).charAt(ii)!=' '){
                                tt+=text.get(i).charAt(ii);
                            }
                        }
                        text.set(i,tt);
                        try{
                            obj[i]=Double.parseDouble(text.get(i));
                        } catch (NumberFormatException e){
                            int pos=i+1;
                            System.out.println("你的第 "+pos+" 个值不能转换成小数！");
                            return;
                        }
                    }
                    else{
                        regStr="'(.*)'";
                        p=Pattern.compile(regStr);
                        m=p.matcher(text.get(i));
                        if (m.find()){
                            obj[i]=m.group(1);
                            String tt=m.group(1);
                            if (tt.length()>=wid.get(i)){
                                int pos=i+1;
                                System.out.println("你的第 "+pos+" 个字符串超出字符数量限制！");
                                return;
                            }
                        }
                        else{
                            int pos=i+1;
                            System.out.println("你的第 "+pos+" 个字符串格式不对，应该用单引号包裹！");
                            return;
                        }
                    }
                }
                FileOutputStream fos=new FileOutputStream(readDbHome()+"/"+classname+".midb",true);
                MyObjectOutputStream oos=new MyObjectOutputStream(fos);
                for (int i=0;i<text.size();i++){
                    SqlResult sqlresult=new SqlResult(obj[i]);
                    oos.writeObject(sqlresult);
                    oos.flush();
                }
                oos.close();
//                RandomAccessFile raf=new RandomAccessFile(readDbHome()+"/"+classname+".midb","rw");
//                for (int i=0;i<text.size();i++){
//                    byte[] bytes=obj[i].toString().getBytes();
//                    if (type.get(i).equals("String")){
//                        String tmp= (String) obj[i];
//                        while (bytes.length< wid.get(i)){
//                            obj[i]=" "+obj[i];
//                            bytes=obj[i].toString().getBytes();
//                        }
//                        obj[i]=tmp;
//                    }
//                    raf.seek(raf.length()+1);
//                    raf.write(bytes);
//                }
//                raf.seek(0);

//                RandomAccessFile raf=new RandomAccessFile(readDbHome()+"/"+classname+".midb","r");
//                int pp=0;
//                while (pp<bytenum.size()){
//                    byte[] b=new byte[bytenum.get(pp)];
//                    int len=0;
//                    if ((len=raf.read(b,0,bytenum.get(pp)))==-1)
//                        break;
//                    System.out.println(new String(b,0,len));
//                    pp++;
//                }
                System.out.println("向数据表 "+classname+" 写入一条记录成功！");
            }
            else{
                System.out.println("数据表 "+classname+" 不存在！");
                return;
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //delete指令
    private void delete(String order,String s) throws IOException, ClassNotFoundException {
        String regStr="delete +from +(.*?) +where +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            int st1=m.start(1);
            int ed1=m.end(1);
            String classname=s.substring(st1,ed1).split(" ")[0];
            boolean flag=false;
            File f=new File(readDbHome());
            File fa[]=f.listFiles();
            for (int i=0;i<fa.length;i++){
                File fs=fa[i];
                String fname=fs.getName();
                if (fname.equals(classname+".midb")){
                    flag=true;
                    break;
                }
            }
            if (flag){
                int st2=m.start(2);
                int ed2=m.end(2);
                String formula=s.substring(st2,ed2);
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                while ((line=br.readLine())!=null){
                    arr.add(line);
                }
                br.close();
                String[] str;
                CopyOnWriteArrayList<String> title=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> type=new CopyOnWriteArrayList<>();
                for (String ss:arr){
                    str=ss.split("\t");
                    if (str[0].equals(classname)){
                        title.add(str[1]);
                        type.add(str[2]);
                    }
                }
                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> table=new CopyOnWriteArrayList<>();
                FileInputStream fis=new FileInputStream(readDbHome()+"/"+classname+".midb");
                MyObjectInputStream ois=new MyObjectInputStream(fis);
                while (fis.available()>0){
                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                    for (int i=0;i<title.size();i++){
                        tmp.add(ois.readObject());
                    }
                    table.add(tmp);
                }
                ois.close();
                int i,j;
                for (i=0;i<formula.length();i++){
                    if (formula.charAt(i)=='<'||formula.charAt(i)=='='||formula.charAt(i)=='>')
                        break;
                }
                String l=formula.substring(0,i);
                for (j=formula.length()-1;j>=0;j--){
                    if (formula.charAt(j)=='<'||formula.charAt(j)=='='||formula.charAt(j)=='>')
                        break;
                }
                String r=formula.substring(j+1);
                String condition=formula.substring(i,j+1);
                formula=l+" "+condition+" "+r;
                regStr="(.*?) +(.*?) +(.*)";
                p=Pattern.compile(regStr);
                m=p.matcher(formula);
                if (m.find()){
                    String match_title=m.group(1);
                    String value=m.group(3);
                    String op=m.group(2);
                    int idx=0;
                    boolean fl=false;
                    for (int x=0;x<title.size();x++){
                        if (match_title.equals(title.get(x))){
                            fl=true;
                            idx=x;
                            break;
                        }
                    }
                    if (fl){
                        CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=new CopyOnWriteArrayList<>();
                        if (type.get(idx).equals("varchar")){
                            regStr="'(.*?)'";
                            p=Pattern.compile(regStr);
                            m=p.matcher(value);
                            if (m.find()){
                                value=m.group(1);
                                if (op.equals("<")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)<0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals(">")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)>0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)==0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals(">=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)>=0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("<=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)<=0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("<>")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (tmp.get(idx).toString().compareTo(value)!=0){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else{
                                    System.out.println("无法识别条件运算符！");
                                    return;
                                }
                            }
                            else{
                                System.out.println("引号格式错误！");
                                return;
                            }
                        }
                        else{
                            try{
                                Double val=Double.parseDouble(value);
                                if (op.equals("<")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())<val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals(">")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())>val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())==val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals(">=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())>=val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("<=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())<=val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else if (op.equals("<>")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx).toString())!=val){
                                            continue;
                                        }
                                        ans.add(tmp);
                                    }
                                }
                                else{
                                    System.out.println("无法识别条件运算符！");
                                    return;
                                }
                            } catch (NumberFormatException e){
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        File of=new File(readDbHome()+"/"+classname+".midb");
                        of.delete();
                        File ff=new File(readDbHome()+"/"+classname+".midb");
                        ff.createNewFile();
                        if (ans.size()!=0){
                            FileOutputStream fos=new FileOutputStream(readDbHome()+"/"+classname+".midb",true);
                            MyObjectOutputStream oos=new MyObjectOutputStream(fos);
                            for (int ii=0;ii<ans.size();ii++){
                                CopyOnWriteArrayList<Object> tmp=ans.get(ii);
                                for (int jj=0;jj<tmp.size();jj++){
                                    SqlResult sqlresult=new SqlResult(tmp.get(jj));
                                    oos.writeObject(sqlresult);
                                    oos.flush();
                                }
                            }
                            oos.close();
                        }
                        int cnt=table.size()-ans.size();
                        System.out.println("共有 "+cnt+" 条记录被删除");
                    }
                    else{
                        System.out.println("列名称不存在！");
                        return;
                    }
                }
                else{
                    System.out.println("格式错误！");
                    return;
                }
            }
            else{
                System.out.println("数据表不存在！");
                return;
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    //update指令
    private void update(String order,String s) throws IOException, ClassNotFoundException {
        String regStr="update +(.*) +set +(.*) +where +(.*)";
        Pattern p=Pattern.compile(regStr);
        Matcher m=p.matcher(order);
        if (m.find()){
            String classname=s.substring(m.start(1),m.end(1)).split(" ")[0];
            String f1=s.substring(m.start(2),m.end(2));
            String f2=s.substring(m.start(3),m.end(3));
            boolean flag=false;
            File f=new File(readDbHome());
            File fa[]=f.listFiles();
            for (int i=0;i<fa.length;i++){
                File fs=fa[i];
                String fname=fs.getName();
                if (fname.equals(classname+".midb")){
                    flag=true;
                    break;
                }
            }
            if (flag){
                FileInputStream in=new FileInputStream(readTableInfo());
                BufferedReader br=new BufferedReader(new InputStreamReader(in));
                String line=null;
                CopyOnWriteArrayList<String> arr=new CopyOnWriteArrayList<>();
                while ((line=br.readLine())!=null){
                    arr.add(line);
                }
                br.close();
                String[] str;
                CopyOnWriteArrayList<String> title=new CopyOnWriteArrayList<>();
                CopyOnWriteArrayList<String> type=new CopyOnWriteArrayList<>();
                for (String ss:arr){
                    str=ss.split("\t");
                    if (str[0].equals(classname)){
                        title.add(str[1]);
                        type.add(str[2]);
                    }
                }
                CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> table=new CopyOnWriteArrayList<>();
                FileInputStream fis=new FileInputStream(readDbHome()+"/"+classname+".midb");
                MyObjectInputStream ois=new MyObjectInputStream(fis);
                while (fis.available()>0){
                    CopyOnWriteArrayList<Object> tmp=new CopyOnWriteArrayList<>();
                    for (int i=0;i<title.size();i++){
                        tmp.add(ois.readObject());
                    }
                    table.add(tmp);
                }
                ois.close();
                int i,j;
                for (i=0;i<f1.length();i++){
                    if (f1.charAt(i)=='<'||f1.charAt(i)=='='||f1.charAt(i)=='>')
                        break;
                }
                String l=f1.substring(0,i);
                for (j=f1.length()-1;j>=0;j--){
                    if (f1.charAt(j)=='<'||f1.charAt(j)=='='||f1.charAt(j)=='>')
                        break;
                }
                String r=f1.substring(j+1);
                String condition=f1.substring(i,j+1);
                f1=l+" "+condition+" "+r;
                for (i=0;i<f2.length();i++){
                    if (f2.charAt(i)=='<'||f2.charAt(i)=='='||f2.charAt(i)=='>')
                        break;
                }
                l=f2.substring(0,i);
                for (j=f2.length()-1;j>=0;j--){
                    if (f2.charAt(j)=='<'||f2.charAt(j)=='='||f2.charAt(j)=='>')
                        break;
                }
                r=f2.substring(j+1);
                condition=f2.substring(i,j+1);
                f2=l+" "+condition+" "+r;
                regStr="(.*?) +(.*?) +(.*)";
                p=Pattern.compile(regStr);
                m=p.matcher(f1);
                if (m.find()&&m.group(2).equals("=")){
                    int idx1=-1;
                    for (int ii=0;ii<title.size();ii++){
                        if (title.get(ii).equals(m.group(1))){
                            idx1=ii;
                            break;
                        }
                    }
                    if (idx1==-1){
                        System.out.println("列名称不存在！");
                        return;
                    }
                    String value1=m.group(3);
                    Double val1=0.0;
                    if (type.get(idx1).equals("varchar")){
                        regStr="'(.*?)'";
                        p=Pattern.compile(regStr);
                        m=p.matcher(value1);
                        if (m.find()){
                            value1=m.group(1);
                        }
                        else{
                            System.out.println("格式错误！");
                            return;
                        }
                    }
                    else{
                        try{
                            val1=Double.parseDouble(value1);
                        } catch (NumberFormatException e){
                            System.out.println("格式错误！");
                            return;
                        }
                    }
                    regStr="(.*?) +(.*?) +(.*)";
                    p=Pattern.compile(regStr);
                    m=p.matcher(f2);
                    if (m.find()){
                        String op=m.group(2);
                        int idx2=-1;
                        for (int ii=0;ii<title.size();ii++){
                            if (title.get(ii).equals(m.group(1))){
                                idx2=ii;
                                break;
                            }
                        }
                        if (idx2==-1){
                            System.out.println("列名称不存在！");
                            return;
                        }
                        String value2=m.group(3);
                        Double val2=0.0;
                        if (type.get(idx2).equals("varchar")){
                            regStr="'(.*?)'";
                            p=Pattern.compile(regStr);
                            m=p.matcher(value2);
                            if (m.find()){
                                value2=m.group(1);
                            }
                            else{
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        else{
                            try{
                                val2=Double.parseDouble(value2);
                            } catch (NumberFormatException e){
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> anss=new CopyOnWriteArrayList<>();
                        int cnt=0;
                        if (type.get(idx2).equals("varchar")){
                            if (op.equals("<")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)<0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else if (op.equals(">")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)>0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else if (op.equals("=")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)==0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else if (op.equals("<=")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)<=0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else if (op.equals(">=")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)>=0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else if (op.equals("<>")){
                                for (int x=0;x<table.size();x++){
                                    CopyOnWriteArrayList<Object> tmp=table.get(x);
                                    if (tmp.get(idx2).toString().compareTo(value2)!=0){
                                        if (type.get(idx1).equals("varchar")){
                                            tmp.set(idx1,value1);
                                        }
                                        else if (type.get(idx1).equals("int")){
                                            tmp.set(idx1,Integer.parseInt(value1));
                                        }
                                        else{
                                            tmp.set(idx1,val1);
                                        }
                                        cnt++;
                                    }
                                    anss.add(tmp);
                                }
                            }
                            else{
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        else{
                            try{
                                if (op.equals("<")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())<val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else if (op.equals(">")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())>val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else if (op.equals("=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())==val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else if (op.equals("<=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())<=val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else if (op.equals(">=")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())>=val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else if (op.equals("<>")){
                                    for (int x=0;x<table.size();x++){
                                        CopyOnWriteArrayList<Object> tmp=table.get(x);
                                        if (Double.parseDouble(tmp.get(idx2).toString())!=val2){
                                            if (type.get(idx1).equals("varchar")){
                                                tmp.set(idx1,value1);
                                            }
                                            else if (type.get(idx1).equals("int")){
                                                tmp.set(idx1,Integer.parseInt(value1));
                                            }
                                            else{
                                                tmp.set(idx1,val1);
                                            }
                                            cnt++;
                                        }
                                        anss.add(tmp);
                                    }
                                }
                                else{
                                    System.out.println("格式错误！");
                                    return;
                                }
                            } catch (NumberFormatException e){
                                System.out.println("格式错误！");
                                return;
                            }
                        }
                        CopyOnWriteArrayList<CopyOnWriteArrayList<Object>> ans=anss;
                        File of=new File(readDbHome()+"/"+classname+".midb");
                        of.delete();
                        File ff=new File(readDbHome()+"/"+classname+".midb");
                        ff.createNewFile();
                        if (ans.size()!=0){
                            FileOutputStream fos=new FileOutputStream(readDbHome()+"/"+classname+".midb",true);
                            MyObjectOutputStream oos=new MyObjectOutputStream(fos);
                            for (int ii=0;ii<ans.size();ii++){
                                CopyOnWriteArrayList<Object> tmp=ans.get(ii);
                                for (int jj=0;jj<tmp.size();jj++){
                                    SqlResult sqlresult=new SqlResult(tmp.get(jj));
                                    oos.writeObject(sqlresult);
                                    oos.flush();
                                }
                            }
                            oos.close();
                        }
                        System.out.println("共有 "+cnt+" 条记录被更新");
                    }
                    else{
                        System.out.println("格式错误！");
                        return;
                    }
                }
                else{
                    System.out.println("格式错误！");
                    return;
                }
            }
            else{
                System.out.println("数据表不存在！");
                return;
            }
        }
        else{
            System.out.println("格式错误！");
            return;
        }
    }
    public TableManager() throws IOException, ClassNotFoundException {
        Welcome();
        System.out.println();
        while (true){
            System.out.print("\n");
            System.out.print("miDB>");
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String s=br.readLine();
            String order=s.toLowerCase();
            if (order.startsWith("quit")){
                quit();
            }
            else if (order.startsWith("help")){
                help();
            }
            else if (order.startsWith("show")){
                show();
            }
            else if (order.startsWith("desc")){
                desc(order,s);
            }
            else if (order.startsWith("create")){
                create(order,s);
            }
            else if (order.startsWith("drop")){
                drop(order,s);
            }
            else if (order.startsWith("select")){
                select(order,s);
            }
            else if (order.startsWith("insert")){
                insert(order,s);
            }
            else if (order.startsWith("delete")){
                delete(order,s);
            }
            else if (order.startsWith("update")){
                update(order,s);
            }
            else{
                System.out.println("不能识别该指令，请检查！");
            }
        }
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        new TableManager();
    }
}

class MyObjectOutputStream extends ObjectOutputStream {

    public MyObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    @Override
    protected void writeStreamHeader() throws IOException {
        //重写读取头部信息方法：不写入头部信息
        super.reset();
    }
}
class MyObjectInputStream extends ObjectInputStream {
    public MyObjectInputStream(InputStream in) throws IOException {
        super(in);
    }

    @Override
    protected void readStreamHeader() throws IOException {
        //重写读取头部信息方法：什么也不做
    }
}